﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BackGround_DeleteInfo : System.Web.UI.Page
{
    Operation operation = new Operation(); //业务类对象
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.GridView1.Attributes.Add("SortExpression", "sysName");
            this.GridView1.Attributes.Add("SortDirection", "DESC");
            if (Session["Admin"] == null)
            {
                WebMessageBox.Show("会话失效，请重新登录！");
            }
            else
            {
                GridViewBind();
            }
        }
    }
    /// <summary>
    /// 绑定用户信息到GridViev控件
    /// </summary>
    
    private void GridViewBind()
    {
        GridView1.DataSource = operation.SearchAdmin("");
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //高亮显示指定行
            e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#FFF000'");
            e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;");
            //删除指定行数据时，弹出询问对话框
            ((LinkButton)(e.Row.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm('是否删除当前行数据！')");
        }
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridViewBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        operation.DeleteAdmin(GridView1.DataKeys[e.RowIndex].Value.ToString());
        GridViewBind();
    }
    protected void BtSearch_Click(object sender, EventArgs e)
    {
        if (umsg.Text != "")
        {
            GridView1.PageIndex = 0;
            GridView1.DataSource = operation.SearchAdmin(umsg.Text.Trim());
            GridView1.DataBind();
        }
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
    }
    protected void sorted(object sender, GridViewSortEventArgs e)
    {
        //if (isSorting) return;
        // isSorting = true;

        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();
        // 假定为排序方向为“顺序”
        //SortDirection sortDirection = SortDirection.Ascending;
        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        string sortDirection = "ASC";
        if (this.GridView1.Attributes["SortExpression"].CompareTo(sortExpression) == 0)
        {
            //获得下一次的排序状态
            if (this.GridView1.Attributes["SortDirection"].CompareTo(sortDirection) == 0)
            {
                sortDirection = "DESC";
            }
        }
        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;
        // 根据GridView排序数据列及排序方向设置显示的默认数据视图
        //WebMessageBox.Show("" + sortExpression);
        if (!string.IsNullOrEmpty(sortExpression))
        {
            this.GridViewBind();
            DataTable dt = ((DataSet)GridView1.DataSource).Tables[0];

            DataView dv = new DataView(dt);
            dv.Sort = string.Format("{0} {1}", sortExpression, sortDirection);

            GridView1.DataSource = dv;
            GridView1.DataBind();
        }
    }
}
